/* Calculate Lee Bounds for various indices for the midline survey, which is plagued with attrition. */
local folder "C:\Users\mlevere\OneDrive - Mathematica\Documents\Projects\Nepal\"

/* Wave 1 households only: Women attrition first */
use "`folder'/Data/midline_raw_women", clear

/* Drop missing/new women */
drop if knowledge_firstmilk == .

local norenamevars = "district vdc vdc_code treatment cash_control info_control vdc_name www hh sn q00_001a control_vdc info_vdc cash_vdc"

qui ds `norenamevars', not
local varnames "`r(varlist)'"
foreach var of varlist `varnames' {
rename `var' `var'_e
}

rename q00_001a idc

tempfile women_data_panel
save "`women_data_panel'", replace

// Keep all women from households that were interviewed at baseline (i.e. no new households)
use "`folder'/Data/baseline_raw_hh.dta", clear
keep www hh district vdc vdc_code
merge 1:m www hh district vdc vdc_code using "`women_data_panel'", nogen keep(3)

save "`women_data_panel'", replace

// Add on baseline information where available
use "`folder'/Data/baseline_raw_women.dta", clear
merge m:1 www hh using "`folder'/Data/baseline_raw_hh.dta", nogen keep(3)
drop expend_total income_total

egen expend_total = rowtotal(expend_fuel-expend_agri_goods)
egen income_total = rowtotal(income_agri-income_prize)

gen expend_total_l = log(expend_total)
gen income_total_l = log(income_total)

local norenamevars = "www hh idc district vdc vdc_code treatment cash_control info_control vdc_name control_vdc info_vdc cash_vdc"

local kvars "age_years school woman_numkids hh_head_age num_hh_members hh_head_male reg_electricity roof separate_kitchen woman_weight woman_height rooms stories hh_land farmland knowledge_firstmilk knowledge_exclusivebreastfeed knowledge_6t11_fed knowledge_12t24_snacks knowledge_breastfeed_diahrrea knowledge_anc_checkups knowledge_iron_supp knowledge_deworming knowledge_vitaminA knowledge_pregnant_food youngest_3days_nonbreastmilk expend_total income_total expend_total_l income_total_l youngest_age_months curr_pregnant"

keep `norenamevars' `kvars'

// Rename the baseline variables we are keeping to have _b at end for simple merging
foreach var of local kvars {
rename `var' `var'_b
}


// Only keep wave 1 households
merge m:1 district vdc_name using "`folder'/Data/wave1_vdcs", nogen keep(3) 

// Merge information about the baseline characteristics for youth (where available) onto the main data
// Recall that we are already only keeping households who were present at baseline 
merge 1:m `norenamevars' using "`women_data_panel'"

gen attrition = _merge == 1 if _merge ~= 2
drop if _merge == 2

merge m:1 www hh using "`folder'/Data/baseline_flag_hh.dta", keep(3) nogen


drop if flag_spending == 1 | flag_missingwomen == 1

svyset vdc_code, strata(district)

gen exclusive_6mo_e = knowledge_exclusivebreastfeed_e == 2 if knowledge_exclusivebreastfeed_e ~= .

gen preg_more_e = knowledge_pregnant_food_e == 1 if knowledge_pregnant_food_e ~= .

gen diarrhea_more_e = knowledge_breastfeed_diahrrea_e == 3 if knowledge_breastfeed_diahrrea_e ~= .

gen givebaby_firstmilk_e = knowledge_firstmilk_e == 1 if knowledge_firstmilk_e ~= .

gen feed_6t11_e = knowledge_6t11_fed_e >= 2 & knowledge_6t11_fed_e <= 4 if knowledge_6t11_fed_e ~= .

gen snacks_12t24_e = knowledge_12t24_snacks_e >= 4 & knowledge_12t24_snacks_e <= 6 if knowledge_12t24_snacks_e ~= .

gen iron_3t4_e = knowledge_iron_supp_e == 3 | knowledge_iron_supp_e == 4 if knowledge_iron_supp_e ~= .

gen deworming_2ndtri_e = knowledge_deworming_e >= 4 & knowledge_deworming_e <= 7 if knowledge_deworming_e ~= .

gen vitaminA_45_e = knowledge_vitaminA_e <= 45 if knowledge_vitaminA_e ~= .

gen anc_checkups_4_e = knowledge_anc_checkups_e == 4 if knowledge_anc_checkups_e ~= .

gen knowledge_index = givebaby_firstmilk_e + exclusive_6mo_e + feed_6t11_e + snacks_12t24_e + diarrhea_more_e + iron_3t4_e + deworming_2ndtri_e + vitaminA_45_e + preg_more_e + anc_checkups_4_e


/* Next up behavior (only done for new kids) */
gen newkids = 1 if woman_numkids_e > woman_numkids_b & youngest_age_months_e <= 13
replace newkids = 1 if curr_pregnant_b == 1 & attrition == 1

gen youngest_irontab_5t6mos = youngest_irontab_num_e >= 150 & youngest_irontab_num_e <= 210

replace youngest_deworming_e = 0 if youngest_deworming_e == 2

replace youngest_vitaminA_e = 0 if youngest_vitaminA_e == 2

gen first_breastfed_0hr = youngest_breastfed_first_e == 0

gen nonightblindness = youngest_nightblindness_e == 0

replace youngest_3days_nonbreastmilk_e = 0 if youngest_3days_nonbreastmilk_e == 2
gen threedays_onlybreastmilk = youngest_3days_nonbreastmilk_e == 0

gen exclusive_breastfed_6mos = youngest_breastfed_exclusive_e == 6 | (youngest_breastfed_exclusive_e == 99 & youngest_age_months_e <= 6)

local demean "youngest_antenatal_care_e youngest_deworming_e youngest_vitaminA_e threedays_onlybreastmilk exclusive_breastfed_6mos first_breastfed_0hr youngest_irontab_5t6mos"
foreach var of local demean {
egen `var'_m_c = mean(`var') if control_vdc == 1 & newkids == 1
egen `var'_sd_c = sd(`var') if control_vdc == 1 & newkids == 1
egen `var'_m = max(`var'_m_c) if newkids == 1
egen `var'_sd = max(`var'_sd_c) if newkids == 1
gen `var'_z = (`var'-`var'_m)/`var'_sd if newkids == 1
}

replace youngest_antenatal_care_e = . if newkids == .

corrmat youngest_antenatal_care_e_z youngest_deworming_e_z youngest_vitaminA_e_z threedays_onlybreastmilk_z exclusive_breastfed_6mos_z first_breastfed_0hr_z youngest_irontab_5t6mos_z, covmat(behav_cov_z)
mat ones_z = J(7,1,1)
mat weights_z = inv(ones_z'*inv(behav_cov_z)*ones_z)
local weight_z = weights_z[1,1]
mat behav_cov_inv_z = inv(behav_cov_z)
foreach col of numlist 1/7 {
local weightvarz`col' = behav_cov_inv_z[1,`col']+behav_cov_inv_z[2,`col']+behav_cov_inv_z[3,`col']+behav_cov_inv_z[4,`col']+behav_cov_inv_z[5,`col']+behav_cov_inv_z[6,`col']+behav_cov_inv_z[7,`col']
}
gen behav_index_z = `weight_z'*(`weightvarz1'*youngest_antenatal_care_e_z + `weightvarz2'* youngest_deworming_e_z + `weightvarz3'* youngest_vitaminA_e_z + `weightvarz4'* threedays_onlybreastmilk_z + `weightvarz5'* exclusive_breastfed_6mos_z + `weightvarz6'* first_breastfed_0hr_z + `weightvarz7'* youngest_irontab_5t6mos_z)

leebounds knowledge_index cash_vdc if info_vdc == 0
leebounds knowledge_index info_vdc if cash_vdc == 0

leebounds behav_index_z cash_vdc if info_vdc == 0 & newkids == 1
leebounds behav_index_z info_vdc if cash_vdc == 0 & newkids == 1

/* Cognitive and Anthropometric Indices - requires the child sample */
use "`folder'/Data/midline_raw_child_z_rc", clear

/* This dataset already has the parent's information. 
   But now add on the household info too */
merge m:1 www hh sn using "`folder'/Data/midline_raw_hh", nogen keep(3)

/* Rename all endline variables, so can merge in the baseline ones too */
local norenamevars = "district q00_001a vdc vdc_code co_code treatment cash_control info_control vdc_name www hh sn control_vdc info_vdc cash_vdc"

qui ds `norenamevars', not
local varnames "`r(varlist)'"
foreach var of varlist `varnames' {
rename `var' `var'_e
}

rename q00_001a idc_child

tempfile child_data_panel
save "`child_data_panel'", replace

// Keep all children from households that were interviewed at baseline (i.e. no new kids in new households)
use "`folder'/Data/baseline_raw_hh.dta", clear
egen expend_total_b = rowtotal(expend_fuel-expend_agri_goods)
egen income_total_b = rowtotal(income_agri-income_prize)

gen expend_total_l_b = log(expend_total)
gen income_total_l_b = log(income_total)

keep www hh district vdc vdc_code expend_total_* income_total_*
merge 1:m www hh district vdc vdc_code using "`child_data_panel'", nogen keep(3)

save "`child_data_panel'", replace

// Add on baseline information for the child where available
use "`folder'/Data/baseline_raw_child_z_rc.dta", clear

merge m:1 www hh using "`folder'/Data/baseline_raw_hh.dta", nogen keep(3)
// Only keep wave 1 households
merge m:1 district vdc_name using "`folder'/Data/wave1_vdcs", nogen keep(3) 

rename q06_idc idc_child

local norenamevars = "www hh idc_child district vdc vdc_code treatment cash_control info_control vdc_name control_vdc info_vdc cash_vdc"

local kvars "_zwei _zlen _zwfl child_tot_months water_source toilet child_sex school"

keep `norenamevars' `kvars'

// Rename the baseline variables we are keeping to have _b at end for simple merging
foreach var of local kvars {
rename `var' `var'_b
}

// Merge information about the baseline characteristics for youth (where available) onto the main data
// Recall that we are already only keeping households who were present at baseline 
merge 1:m www hh idc_child using "`child_data_panel'"

merge m:1 www hh using "`folder'/Data/baseline_flag_hh.dta", keep(3) nogen
gen attrition = _merge == 1 if _merge ~= 2
replace attrition = 1 if asq_communication_e == . & child_height_e == . & child_weight_e == .

drop if _merge == 2
drop if flag_spending == 1 | flag_missingwomen == 1


gen child_age = child_tot_months_b/12
keep if child_age <= 2

svyset vdc_code, strata(district)

gen child_tot_buckets = child_tot_months_e
replace child_tot_buckets = child_tot_months_e + 1 if mod(child_tot_months_e,2) == 1 & child_tot_months_e <= 24
replace child_tot_buckets = child_tot_months_e + (3-mod(child_tot_months_e,3)) if child_tot_months_e >= 25 & child_tot_months_e <= 36 & mod(child_tot_months_e,3) ~= 0
replace child_tot_buckets = child_tot_months_e + (6-mod(child_tot_months_e,6)) if child_tot_months_e >= 37 & mod(child_tot_months_e,6) ~= 0
tab child_tot_buckets, gen(child_tot_months)

gen child_tot_buckets_b = child_tot_months_b
replace child_tot_buckets_b = child_tot_months_b + 1 if mod(child_tot_months_b,2) == 1 & child_tot_months_b <= 24
replace child_tot_buckets_b = child_tot_months_b + (3-mod(child_tot_months_b,3)) if child_tot_months_b >= 25 & child_tot_months_b <= 36 & mod(child_tot_months_b,3) ~= 0
replace child_tot_buckets_b = . if child_tot_months_b > 36
tab child_tot_buckets_b, gen(child_tot_months_b)

tab interviewer_name, gen(enumerator_fe)


gen underweight_e = _zwei_e < -2 if _zwei_e ~= .
gen stunted_e = _zlen_e < -2 if _zwei_e ~= .
gen wasted_e = _zwfl_e < -2 if _zwei_e ~= .

// Re-scale ASQ variables so that they have mean 0/standard deviaition of 1 in the control group
local asqvars "communication grossmotor finemotor personalsocial problemsolving"

foreach asq of local asqvars {
by child_tot_buckets, sort: egen `asq'_m_c = mean(asq_`asq'_e) if control_vdc == 1 & child_tot_months_e <= 36 
by child_tot_buckets, sort: egen `asq'_sd_c = sd(asq_`asq'_e) if control_vdc == 1 & child_tot_months_e <= 36 
by child_tot_buckets, sort: egen `asq'_sd = max(`asq'_sd_c) if child_tot_months_e <= 36
by child_tot_buckets, sort: egen `asq'_m = max(`asq'_m_c) if child_tot_months_e <= 36
gen asq_`asq'_z = (asq_`asq'_e-`asq'_m)/`asq'_sd if child_tot_months_e <= 36

}

// Create indexes as in Anderson 2008: ASQ index
corrmat asq_communication_z asq_grossmotor_z asq_finemotor_z asq_problemsolving_z asq_personalsocial_z, covmat(cog_cov)
mat ones = J(5,1,1)
mat weights = inv(ones'*inv(cog_cov)*ones)
local weight = weights[1,1]
mat cog_cov_inv = inv(cog_cov)
foreach col of numlist 1/5 {
local weightvar`col' = cog_cov_inv[1,`col']+cog_cov_inv[2,`col']+cog_cov_inv[3,`col']+cog_cov_inv[4,`col']+cog_cov_inv[5,`col']
}
gen asq_index = `weight'*(`weightvar1'*asq_communication_z + `weightvar2'*asq_grossmotor_z + `weightvar3'*asq_finemotor_z + `weightvar4'*asq_problemsolving_z + `weightvar5'*asq_personalsocial_z)

gen male = child_sex_b == 1 if child_sex_b ~= .
replace school_b = 0 if school_b == 2




/* Anthropometrics */
gen underweight_b = _zwei_b < -2 if _zwei_b ~= .
gen stunted_b = _zlen_b < -2 if _zwei_b ~= .
gen wasted_b = _zwfl_b < -2 if _zwei_b ~= .
// Create indexes as in Anderson 2008: Anthropometric index (all variables should have a "positive" lens first, i.e., not stunted instead of stunted)
gen notstunted = stunted_e == 0
replace notstunted = . if stunted_e == .

gen notunderweight = underweight_e == 0
replace notunderweight = . if underweight_e == .

gen notwasted = wasted_e == 0
replace notwasted = . if wasted_e == .

gen notsick = child_sick_e == 0

local demean "notstunted notunderweight notwasted notsick"
foreach var of local demean {
egen `var'_m_c = mean(`var') if control_vdc == 1 
egen `var'_sd_c = sd(`var') if control_vdc == 1 
egen `var'_sd = max(`var'_sd_c)
egen `var'_m = max(`var'_m_c)
gen `var'_z = (`var'-`var'_m)/`var'_sd
}


corrmat notstunted_z notunderweight_z notwasted_z notsick_z, covmat(anth_cov)
mat ones = J(4,1,1)
mat weights = inv(ones'*inv(anth_cov)*ones)
local weight = weights[1,1]
mat anth_cov_inv = inv(anth_cov)
foreach col of numlist 1/4 {
local weightvar`col' = anth_cov_inv[1,`col']+anth_cov_inv[2,`col']+anth_cov_inv[3,`col']+anth_cov_inv[4,`col']
}
gen anth_index = `weight'*(`weightvar1'*notstunted_z + `weightvar2'*notunderweight_z + `weightvar3'*notwasted_z + `weightvar4'*notsick_z)

leebounds asq_index cash_vdc if info_vdc == 0
leebounds asq_index info_vdc if cash_vdc == 0

leebounds anth_index cash_vdc if info_vdc == 0
leebounds anth_index info_vdc if cash_vdc == 0


